/**
 * 全局变量：跳转splunk搜索页面的url
 */
var searchUrl = "";


// ---------------------- 事件列表页需要 ---------------------
function adjustAlertTable() {
    // 动态调整列宽
    $("table").colResizable();
    // 给表格添加点击事件
    $("#alertTable tr").each(function (i, n) {
        if (i != 0) {
            $(this).find("td[class!='operation']").click(function () {
                var dom = $(this).parents("tr").find("td[class='checkNot']").find("input");
                if ($(dom).is(":checked")) {
                    $(dom).prop("checked", false);
                }else{
                    $(dom).prop("checked", "checked");
                }
                manucheck(dom);
            });
            $(this).find("td[class='operation']").click(function () {
                var dom = $(this).parents("tr").find("td[class='checkNot']").find("input");
                if ($(dom).is(":checked")) {
                    $(dom).prop("checked", false);
                }else{
                    $(dom).prop("checked", "checked");
                }
            });
        }
    })
}

// ----------------- 告警列表，事件列表页共享------------------
// 查看事件
function ev_alertTable(btn) {
    var alertSid = $(btn).parents("tr").attr("nodeid");
    layer.open({
        type: 2,
        area: ['800px', '480px'],
        maxmin: true,
        title:'事件列表',
        offset:"20px",
        shadeClose: true,
        content: basePath + "/splunk/alert/initAlertEvents.do?alertSid=" + alertSid
    });
}

// 查看日志
function see_alertTable(btn) {
    $("#logTable").empty();
    $("#detailsTable").empty();
    var alertSid = $(btn).parents("tr").attr("nodeid");
    $.ajax({
        url:basePath + "/splunk/alert/initAlertLogs.do",
        type:"post",
        dataType:"json",
        data:{
            alertSid:alertSid
        },
        success:function (json) {
            var code = json.code;
            if (code == '200'){
                var tableSource = json.tableSource;
                var tableData = json.tableData;
                if (tableSource != null && tableSource != ""){
                    showTables(tableSource, tableData);
                }

                var eventSource = json.eventSource;
                var eventData = json.eventData;
                if (eventSource != null && eventSource != "") {
                    showEvents("detailsTable", eventSource, eventData);
                }


            }else if (code == '201'){
                var errorMsg = json.errorMessage;
                layer.msg(errorMsg, {time: 1500});
            }
        }
    })
}

// 跳转到splunk搜索框
function search_alertTable(btn) {
    var alertSid = $(btn).parents("tr").attr("nodeid");
    $.ajax({
        url:basePath + "/splunk/alert/getAlertBySid.do",
        type:"post",
        dataType:"json",
        data:{
            alertSid:alertSid
        },
        success:function (json) {
            var code = json.code;
            if (code == '200') {
                var address = json.address;
                var domain = json.domain;
                var alert = json.data;
                var earliestUnix = json.earliestUnix;
                var latestUnix = json.latestUnix;
                var url = address +
                        "q=" + alert.alertSearch +
                        "&earliest=" + earliestUnix +
                        "&latest=" + latestUnix;
                // H5 postMessage 跨域发送信息
                top.postMessage(url, domain);
            }
        }
    })

}

// 查看更多告警
function rela_alertTable(btn) {
    clearAllTable();
    var alertSid = $(btn).parents("tr").attr("nodeid");
    var alertName = $("#alertName").val();
    var alertLevel = $("#alertLevel").val();
    var earliestTime = $("#earliestTime").val();
    var latestTime = $("#latestTime").val();
    var columns = [{code:"alertSid", name:"checkBox"},
        {code:"tempAlertName", name:"告警名称"},
        {code:"triggerTimeStr", name:"告警触发时间"},
        {code:"alertType", name:"告警类型"},
        {code:"alertOwner", name:"告警所属者"},
        {code:"alertLevelInfo", name:"告警级别"},
        {code:"alertStateInfo", name:"告警状态"},
        {code:"eventSorts", name:"关联事件名称"},
        {code:{S:"查看日志", EV:"查看绑定事件", SE:"搜索"}, name:"操作"}];
    var param = {url:basePath + "/splunk/alert/getMoreAlerts.do",divID:"moreAlertTable",afterQuery:"adjustMoreTable", message:"暂时没有告警信息"};
    var data = {"pageNo":1, "pageSize":15, alertSid:alertSid, alertName:alertName, alertLevel:alertLevel, earliestTime:earliestTime, latestTime:latestTime};
    easyQuery(columns, param, data);
}

function adjustMoreTable() {
    // 动态调整列宽
    $("table").colResizable();
    $.each(alertSidArr, function (i, sid) {
        $("input:checkbox[name='pageItem']").each(function (i, input) {
            if ($(this).attr("nodeid") == sid) {
                $(this).prop("checked", "checked");
                $(this).parents("tr").addClass("timeoutTr");
            }
        })
    })

    // 给表格添加点击事件
    $("#moreAlertTable tr").each(function (i, n) {
        if (i != 0) {
            $(this).find("td[class!='operation']").click(function () {
                var dom = $(this).parents("tr").find("td[class='checkNot']").find("input");
                if ($(dom).is(":checked")) {
                    $(dom).prop("checked", false);
                }else{
                    $(dom).prop("checked", "checked");
                }
                manucheck(dom);
            });
            $(this).find("td[class='operation']").click(function () {
                var dom = $(this).parents("tr").find("td[class='checkNot']").find("input");
                if ($(dom).is(":checked")) {
                    $(dom).prop("checked", false);
                }else{
                    $(dom).prop("checked", "checked");
                }
            });
        }
    })
}

function showEvents(btn, logSource, alertData){
    var div = '<div class="control-title">' +
        '<div class="title_left"> ' +
        '<span> ' +
        '   <svg class="icon" aria-hidden="true"> ' +
        '       <use xlink:href="#icon-biaoti"></use> ' +
        '   </svg> ' +
        '</span>' +
        '日志来源：' + logSource+
        '</div>' +
        '</div>';
    div += '<div class="content_table"> ' +
        '   <table id="logTable1" class="table table-striped table-bordered table-hover"> ' +
        '   <thead> ' +
        '       <tr> ' +
        '           <th>详细日志数据</th> ' +
        '       </tr> ' +
        '   </thead> ' +
        '   <tbody>';

    $.each(alertData, function (i, data) {
        div+='<tr><td>' + data.alertData + "</td></tr>";
    });
    div += '</tbody></table></div>';
    $("#" + btn).append(div);
}

function showTables(tableSource, tableData) {
    var div = '<div class="control-title">' +
        '<div class="title_left"> ' +
        '<span> ' +
        '   <svg class="icon" aria-hidden="true"> ' +
        '       <use xlink:href="#icon-biaoti"></use> ' +
        '   </svg> ' +
        '</span>' +
        '告警来源：'+ tableSource +
        '</div>' +
        '</div>' +
        '<div class="content_table"> ' +
        '   <table id="logTable1" class="table table-striped table-bordered table-hover "> ' +
        '   <thead> ' +
        '       <tr>';
    $.each(tableData, function (i, data) {
        if (i == 0) {
            $.each(data.map, function (key, value) {
                div += '<th>' + key + '</th>';
            });
            div += '</tr></thead><tbody><tr>';
        }
        $.each(data.map, function (key, value) {
            div+='<td>' + value + '</td>';
        })
        div += "</tr>";
    });
    div += '</tbody></table></div>';
    $("#logTable").append(div);
}

function see_moreAlertTable(btn){
    see_alertTable(btn);
}

function ev_moreAlertTable(btn) {
    ev_alertTable(btn);
}

function search_moreAlertTable(btn) {
    search_alertTable(btn);
}

// 清空三个div
function clearAllTable() {
    $("#moreAlertTable").empty();
    $("#logTable").empty();
    $("#detailsTable").empty();
}




